
package msu.erdc.hci;

import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Toast;

public class Contacts extends Fragment{
	private Button eButton;
	private Button aButton;
	private Button iButton;
	private Button rButton;
	private Button addButton;
//	private Button callButton;
	//public static int counter=0;

	//private EditText editlname;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		
		View view =  inflater.inflate(R.layout.contacts, container, false);
		// editlname = (EditText) view.findViewById(R.id.nameText);
		
		aButton = (Button) view.findViewById(R.id.accidentcontactButton);
		aButton.setOnClickListener(unsupportedClick);
		iButton = (Button) view.findViewById(R.id.agentcontactButton);
		iButton.setOnClickListener(unsupportedClick);
		rButton = (Button) view.findViewById(R.id.roadsideasstcontactButton);
		rButton.setOnClickListener(unsupportedClick);
		
		 eButton = (Button) view.findViewById(R.id.emergencycontactButton);
		 addButton = (Button) view.findViewById(R.id.addcontactButton);
		 // if emergency button is clicked
		 if(eButton.isClickable()==true){
		 eButton.setOnClickListener(new OnClickListener(){
							public void onClick(View v) {
								FragmentTransaction ft = getFragmentManager().beginTransaction();
								ft.replace(android.R.id.content, new ContactList(), "contactList");
								ft.addToBackStack(null);
								ft.commit();
							}});
		 
		 
		 }
		// if add button is clicked
		 if(addButton.isClickable()==true){
		addButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				newButtonClicked();
			}
		});
		}
		return view;
	}
	
	OnClickListener unsupportedClick = new OnClickListener(){

		@Override
		public void onClick(View v) {
			showUnsupportedToast();
		}};
		
	private void showUnsupportedToast()
	{
		Toast.makeText(getActivity(), "Only Emergency Contacts work in the prototype", Toast.LENGTH_SHORT).show();
	}
	
	public void newButtonClicked()
	{
		
		new AlertDialog.Builder(getActivity())
		.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, new String[]{"Insurance Agents","Roadside Assistance","Accident Contacts","Emergeny Contacts"}), new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {

				if(which!=3)//an actual list item clicked
		        {
					showUnsupportedToast();
		        }
		        else
		        {
		        	FragmentTransaction ft = getFragmentManager().beginTransaction();
					ft.replace(android.R.id.content, new AContact(), "contacts");
					ft.addToBackStack(null);
					ft.commit();	
		        }
			}
		})
		.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				//do nothing
			}
		})
		.show();
		
	}
	
	public void setName()
	{
		
		
	}
	

}